package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.*;
import com.qf.service.MailboxService;
import com.qf.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("mailbox")
public class MailboxController {

    // 保修对象
    @Autowired
    private MailboxService mailboxService;


    // 保修管理
    @RequestMapping("mailboxList")
    @ResponseBody
    public JSONResult repairList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "limit", required = false, defaultValue = "5") Integer pageSize
    ) {

        // 设置分页条件
        PageHelper.startPage(pageNum, pageSize);

        List<Mailbox> mailboxList = mailboxService.findByRepairPage();

        PageInfo<Mailbox> pageInfo = new PageInfo<>(mailboxList);

        JSONResult jsonResult = new JSONResult();
        jsonResult.setCode(0);
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setMsg("success");
        jsonResult.setData(mailboxList);
//        System.out.println(jsonResult);
        return jsonResult;


//        model.addAttribute("pageInfo",pageInfo);
//
//        return "listrepair.html";
    }


    // 添加

    /**
     * 前端ajax异步查询所有的community
     *
     * @return
     */
    @RequestMapping("addMailbox1")
    @ResponseBody
    public JSONResult addRepair1() {

        List<Community> communityList = mailboxService.findAllCommunity();

        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(communityList);

        return jsonResult;
    }

    // 添加
    @RequestMapping("addMailbox2")
    @ResponseBody
    public JSONResult addRepair2() {

        List<Owner> ownerList = mailboxService.findAllOwner();

        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(ownerList);

        return jsonResult;
    }


    // 添加或者修改之后跳转

    /**
     * 添加信件，查看信件用到此方法
     *
     * @param mailbox 前端传递过来的邮件信息   添加传递过来的数据不包含id，即id为空，修改包含ID，在service层进行判断
     * @return
     */
    @RequestMapping("saveOrUpdateMailbox")
    @ResponseBody
    public JSONResult saveOrUpdateMailbox(Mailbox mailbox) {

        System.out.println(mailbox);

        JSONResult jsonResult = new JSONResult();
        jsonResult.setCode(10000);
        mailboxService.saveOrUpdateMailbox(mailbox);

        return jsonResult;
    }


    // 修改
    // 暂时没用到
    @RequestMapping("updateMailbox")
    @ResponseBody
    public JSONResult updateMailbox(Integer id) {

        Mailbox mailbox = mailboxService.findByMialboxId(id);

        System.out.println(mailbox);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(mailbox);

        return jsonResult;
    }


    // 删除
    // 无此功能
    @RequestMapping("deleteMailbox")
    @ResponseBody
    public String deleteMailbox(Integer id) {

        mailboxService.deleteMailbox(id);

        return "success";
    }

    // 批量删除
    // 暂时没用到
    @RequestMapping("deleteByMailboxId")
    @ResponseBody
    public String deleteByMailboxId(Integer[] ids) {
        mailboxService.deleteByMailboxId(ids);
        return "success";
    }

    // 根据id查询单个
    // 暂时没用到
    @RequestMapping("findByMailboxId")
    @ResponseBody
    public JSONResult findByMailboxId(Integer id) {

        Mailbox byMialboxId = mailboxService.findByMialboxId(id);

        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(byMialboxId);
        jsonResult.setCode(0);

        return jsonResult;
    }


    // 查看详情

    /**
     * @param id 前端传递过来的id
     * @return
     */
    @RequestMapping("editMailbox")
    @ResponseBody
    public String editMailbox(Integer id) {
        // 根据id查询到对应的邮箱信息
        Mailbox byMialboxId = mailboxService.findByMialboxId(id);
        // 将查询到的邮箱信息状态设为已读
        byMialboxId.setState(1);
        // 修改数据库中邮箱状态
        mailboxService.saveOrUpdateMailbox(byMialboxId);

        return "success";
    }


}